﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.Runtime.InteropServices;


namespace RemoteDesktops
{
    using System;
    using System.Text;
    using System.Collections;
    using System.Runtime.InteropServices;


    public class desktopWindow
    {

        

        public string title;
        public IntPtr hWnd;

        public desktopWindow[] childWindows;

        public override string ToString()
        {
            return title.Substring(0, title.Length - " - Remote Desktop".Length);
        }

        public void restore()
        {
            WINDOWPLACEMENT currWinP = new WINDOWPLACEMENT();
            if (hWnd == null) return;
            // 'get the info...
            if (MyWindowsFunctions.GetWindowPlacement(hWnd,  ref currWinP)) 
             {
                //'based on the returned info,
                //'determine the window state
                 if (currWinP.showCmd == MyWindowsFunctions.SW_SHOWMINIMIZED) 
                 {
                    // 'it is minimized, so restore it
                    currWinP.flags = 0;
                    currWinP.showCmd = MyWindowsFunctions.SW_SHOWNORMAL;
                    MyWindowsFunctions.SetWindowPlacement(hWnd, ref currWinP);
                 }
                 else
                 {
                 ////  'it is on-screen, so make it visible
                 //  // MyWindowsFunctions.SetForegroundWindow(hWnd);
                    MyWindowsFunctions.BringWindowToTop(hWnd);
                 }
                 MyWindowsFunctions.SetActiveWindow(hWnd);
             }
        }

        public void poke()
        {
            foreach (desktopWindow win in childWindows)
            {
                if (win.title.Contains("Remote Desktop"))
                {
                    IntPtr ip1 = new IntPtr(0x1);
                    MyWindowsFunctions.SendMessage(win.hWnd, MyWindowsFunctions.WM_ACTIVATE, ip1, IntPtr.Zero);
                }
            }
//
      //      MyWindowsFunctions.SendMessage(this.hWnd, MyWindowsFunctions.WM_KEYDOWN, new IntPtr(MyWindowsFunctions.VK_A), new IntPtr(0));
          
       
        }

    }

    public struct WINDOWPLACEMENT
    {
        public int length;
        public int flags;
        public int showCmd;
        public System.Drawing.Point ptMinPosition;
        public System.Drawing.Point ptMaxPosition;
        public System.Drawing.Rectangle rcNormalPosition;
    }




    /// &lt;summary>
    /// MyWindowsFunctions Demo - shows the caption of all desktop windows.
    /// Authors: Svetlin Nakov, Martin Kulov
    /// Bulgarian Association of Software Developers - http://www.devbg.org/en/
    /// &lt;/summary>
    ///
    public class MyWindowsFunctions
    {

        public const int SW_SHOWNORMAL = 1;
        public const int SW_SHOWMINIMIZED = 2;
        public const int SW_SHOWMAXIMIZED = 3;
        public const int SW_SHOWNOACTIVATE = 4;


        const int MAXTITLE = 255;

        private static ArrayList mTitlesList;

        private delegate bool EnumDelegate(IntPtr hWnd, int lParam);


        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);

        public const int WM_KEYDOWN = 0x100;
        public const int VK_A = 0x41;
        public const uint WM_ACTIVATE = 0x06;
        
        
        
        
        [DllImport("user32.dll")]
        public static extern IntPtr SetActiveWindow(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr BringWindowToTop(IntPtr hWnd);


        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPlacement(IntPtr hWnd,
           [In] ref WINDOWPLACEMENT lpwndpl);

        [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
         ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool _EnumDesktopWindows(IntPtr hDesktop,
        EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "EnumChildWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool _EnumChildWindows(IntPtr hwndParent, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);


        [DllImport("user32.dll", EntryPoint = "GetWindowText",
         ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int _GetWindowText(IntPtr hWnd,
        StringBuilder lpWindowText, int nMaxCount);

        private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
        {
            string title = GetWindowText(hWnd);
            if (title == "") return true;
            desktopWindow dw = new desktopWindow();
            dw.title = title;
            dw.hWnd = hWnd;
            mTitlesList.Add(dw);
            return true;
        }

        private static bool EnumChildWindowsProc(IntPtr hWnd, int lParam)
        {
            string title = GetWindowText(hWnd);
            if (title == "") return true;
            desktopWindow dw = new desktopWindow();
            dw.title = title;
            dw.hWnd = hWnd;
            mTitlesList.Add(dw);
            return true;
        }

        /// <summary>
        /// Returns the caption of a windows by given HWND identifier.
        /// </summary>
        public static string GetWindowText(IntPtr hWnd)
        {
            StringBuilder title = new StringBuilder(MAXTITLE);
            int titleLength = _GetWindowText(hWnd, title, title.Capacity + 1);
            title.Length = titleLength;

            return title.ToString();
        }

        /// <summary>
        /// Returns the caption of all desktop windows.
        /// </summary>
        public static desktopWindow[] GetDesktopWindowsCaptions()
        {
            mTitlesList = new ArrayList();
            EnumDelegate enumfunc = new EnumDelegate(EnumWindowsProc);
            IntPtr hDesktop = IntPtr.Zero; // current desktop
            bool success = _EnumDesktopWindows(hDesktop, enumfunc, IntPtr.Zero);

            if (success)
            {
                // Copy the result to string array
                desktopWindow[] windows = new desktopWindow[mTitlesList.Count];
                mTitlesList.CopyTo(windows);
                return windows;
            }
            else
            {
                // Get the last Win32 error code
                int errorCode = Marshal.GetLastWin32Error();

                string errorMessage = String.Format(
                "MyWindowsFunctions failed with code {0}.", errorCode);
                throw new Exception(errorMessage);
            }
        }

        /// <summary>
        /// Returns the caption of all desktop windows.
        /// </summary>
        public static desktopWindow[] GetChildWindowsCaptions(IntPtr hWnd)
        {
            mTitlesList = new ArrayList();
            EnumDelegate enumfunc = new EnumDelegate(EnumChildWindowsProc);
            bool success = _EnumChildWindows(hWnd, enumfunc, IntPtr.Zero);

          //  if (success)
           // {
                // Copy the result to string array
                desktopWindow[] windows = new desktopWindow[mTitlesList.Count];
                mTitlesList.CopyTo(windows);
                return windows;
           // }
            //else
           // {
                //// Get the last Win32 error code
                //int errorCode = Marshal.GetLastWin32Error();

                //string errorMessage = String.Format(
                //"MyWindowsFunctions child failed with code {0}.", errorCode);
                //throw new Exception(errorMessage);
          //  }
        }
    }
}
